package com.hdl.design.pattern;

import java.util.Scanner;

/**
 * 类名 SimpleFactoryDemo003
 * 描述 需要使用简单工厂，第一个需要优化的例子
 * 题目：控制台上输入两个数字和一个运算符，在控制台打印结果
 *  分析：SimpleFactoryDemo004程序问题如下：
 *  1、如何调用呢
 *  2、引入简单工厂方法
 *  如下改进：
 *
 * @author hedonglin
 * @version 1.0
 * @date 2019/9/6 21:59
 */
public class SimpleFactoryDemo005 {

    public static void main(String[] args) throws Exception {

        System.out.println("请输入数字A:");
        Scanner scanner = new Scanner(System.in);
        double num1 = scanner.nextDouble();

        System.out.println("请输入运算符:");
        scanner = new Scanner(System.in);
        String operate = scanner.next();

        System.out.println("请输入数字B:");
        scanner = new Scanner(System.in);
        double num2 = scanner.nextDouble();

        // 使用简单工厂实例化哪一个类呢
        OperateFactory.Operate operate1 = OperateFactory.createOperate(operate);
        operate1.setLeftNum(num1);
        operate1.setRightNum(num2);

        double result = operate1.getResult();

        System.out.println("结果是：" + result);
    }


    static class OperateFactory{
        public static Operate createOperate(String operate){
            Operate operattion = null;

            switch (operate) {
                case "+":
                    operattion = new OperateAdd();
                    break;
                case "-":
                    operattion = new OperateSub();
                    break;
                case "*":
                    operattion = new OperateMul();
                    break;
                case "/":
                    operattion = new OperateDiv();
                    break;
                default:
                    break;

            }
            return operattion;
    }


    static class OperateDiv extends Operate{
        @Override
        public double getResult() throws Exception {
            double rightNum = super.getRightNum();
            double leftNum = super.getLeftNum();
            if (rightNum != 0){
                return leftNum / rightNum;
            }else {
                throw new Exception("除数不能为0");
            }
        }
    }

    static class OperateMul extends Operate{
        @Override
        public double getResult() {
            double rightNum = super.getRightNum();
            double leftNum = super.getLeftNum();
            return leftNum * rightNum;
        }
    }

    static class OperateSub extends Operate{
        @Override
        public double getResult() {
            double rightNum = super.getRightNum();
            double leftNum = super.getLeftNum();
            return leftNum - rightNum;
        }
    }

    static class OperateAdd extends Operate{
        @Override
        public double getResult() {
            double rightNum = super.getRightNum();
            double leftNum = super.getLeftNum();
            return leftNum + rightNum;
        }
    }


    static abstract class Operate{
        private double leftNum;
        private double rightNum;

        public abstract double getResult() throws Exception;

        public double getLeftNum() {
            return leftNum;
        }

        public void setLeftNum(double leftNum) {
            this.leftNum = leftNum;
        }

        public double getRightNum() {
            return rightNum;
        }

        public void setRightNum(double rightNum) {
            this.rightNum = rightNum;
        }
    }

}
}
